// addActivityBrowseRecord
exports.addActivityBrowseRecord = function (req, res) {

    //获取参数
    var bodyParams = req.routeInfo.bodyParams;
    //最终结果
    var resultData = {};

    var recordData = 0;

    //任务流创建
    async.waterfall([
            //1.查询浏览记录
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganizationActivityBrowseRecord + "/oabr_oaid/" + bodyParams.oabr_oaid + "/oabr_uid/" + bodyParams.oabr_uid,
                        method: 'POST',
                        data: {
                            fields: ["oabr_id","oabr_times"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        console.log(data);
                        recordData = data.data;
                        cb();
                    }
                );
            },

            function (cb) {
                if (recordData.length>0) {
                    console.log("记录已生成");
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.updateOrganizationActivityBrowseRecord,
                            method: 'POST',
                            data: {
                                oabr_id : recordData[0].oabr_id,
                                oabr_times : parseInt(recordData[0].oabr_times)+1,
                                oabr_create_time : Date.parse(new Date())*0.001
                            }
                        },
                        function (err, data) {
                            //判断是否有错误信息
                            if (err) {
                                return RES.response(res, false, data);
                            }
                            resultData.updateData = data;
                            cb();
                        }
                    );
                }else{
                    console.log("记录正在生成");
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.addOrganizationActivityBrowseRecord,
                            method: 'POST',
                            data: {
                                oabr_oaid: bodyParams.oabr_oaid,
                                oabr_uid: bodyParams.oabr_uid,
                                oabr_oid: bodyParams.oabr_oid
                            }
                        },
                        function (err, data) {
                            //判断是否有错误信息
                            if (err) {
                                return RES.response(res, false, data);
                            }
                            resultData.addData = data;
                            cb();
                        }
                    );
                }
            }
        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

}